import { ChannelData } from "../ChannelData";
import { ChannelBase } from "../base/ChannelBase";

export class BytedanceChannel extends ChannelBase {
    /**初始化渠道 */
    protected InitChannel() {

        if (ChannelData.isHasAd && !ChannelData.isDev) {
            this.CreateVideo();
        }

        this.InitRecorder();
    }

    /**创建插屏广告 */
    public CreatePage() {
        throw new Error("Method not implemented.");
    }
    public ShowPage(on_successed: Function, on_failed: Function, caller: any) {
        throw new Error("Method not implemented.");
    }
    public CreateVideo() {

        if(!tt.createRewardedVideoAd || !this.video_id_list || this.video_id_list.length <= 0)
        {
            return;
        }

        this.video_obj = tt.createRewardedVideoAd({
            adUnitId: this.video_id_list[0]
        });
        this.video_obj.load();
        this.video_obj.onClose(res => {
            if (res.isEnded) {
                if (this.video_successed != null && this.video_successed != undefined) {
                    this.video_successed.call(this.video_caller);
                }
            } else {
                if (this.video_failed != null && this.video_failed != undefined) {
                    this.video_failed.call(this.video_caller);
                }
            }
            if (!!this.video_complete) this.video_complete.call();
        });
        this.video_obj.onError(res => {
            if (this.video_failed != null && this.video_failed != undefined) {
                this.video_failed.call(this.video_caller);
            }
            console.error(res.errMsg + '----' + res.errCode);
        });
    }
    public ShowVideo(on_successed: Function, on_failed: Function, on_complete: Function, caller: any) {
        
        if(!this.video_obj)
        {
            if (on_failed != null && on_failed != undefined)
            {
                on_failed.call(caller);
            }

            return;
        }

        this.video_successed = on_successed;
        this.video_failed = on_failed;
        this.video_caller = caller;
        this.video_complete = on_complete;
        this.video_obj.show().catch(err => {
            this.video_failed?.call(caller);
            if (!!this.video_complete) this.video_complete.call();
        });
    }
    public ShowBanner(on_successed: Function, on_failed: Function, caller: any) {
        throw new Error("Method not implemented.");
    }
    public HideBanner() {
        throw new Error("Method not implemented.");
    }
    public CreateBox() {
        throw new Error("Method not implemented.");
    }
    public ShowBox(on_successed: Function, on_failed: Function, caller: any) {
        throw new Error("Method not implemented.");
    }
    public HideBox() {
        throw new Error("Method not implemented.");
    }
    public CreateJuggle() {
        throw new Error("Method not implemented.");
    }
    public ShowJuggle(on_successed: Function, on_failed: Function, caller: any) {
        throw new Error("Method not implemented.");
    }
    public HideJuggle() {
        throw new Error("Method not implemented.");
    }
    public CreateNative() {
        throw new Error("Method not implemented.");
    }
    public ShowNative(on_successed: Function, on_failed: Function, caller: any) {
        throw new Error("Method not implemented.");
    }
    public HideNative() {
        throw new Error("Method not implemented.");
    }
    public ShareGame() {
        throw new Error("Method not implemented.");
    }

    /**开始录屏 */
    public StartRecord() {
        if (!ChannelData.is_recording && this.recorder) {
            ChannelData.is_recording = true;
            this.recorder.start({
                duration: 300
            });
        } else if (!this.recorder) {
            this.InitRecorder();
            ChannelData.is_recording = true;
            this.recorder.start({
                duration: 300
            });
        }
    }

    /**停止录屏 */
    public StopRecord(on_stop: Function) {
        if (this.recorder && ChannelData.is_recording) {
            ChannelData.is_recording = false;
            this.recorder.stop();
            this.on_record_stop = on_stop;
        }
    }

    /**分享录屏 */
    public ShareRecord(on_successed: Function, on_failed: Function) {
        if (ChannelData.record_path) {
            tt.shareAppMessage({
                //channel: "video",
                //title: "视频分享",
                //desc: ChannelData.share_title,
                //imageUrl: "",
                templateId: "1m88g6k72dnn70m9cs",
                //query: "",
                extra: {
                    videoPath: ChannelData.record_path,
                    videoTopics: [ChannelData.record_topics_1, ChannelData.record_topics_2],
                    hashtag_list: [ChannelData.record_topics_1, ChannelData.record_topics_2],
                    //video_title: ChannelData.share_title, //生成的默认内容
                },
                success() {
                    ChannelData.record_path = null;
                    ChannelData.is_recording = false;
                    //ChannelManager.GetInstance().ShowToast("分享视频成功！");
                    if (on_successed) on_successed();
                },
                fail(err) {
                    //ChannelManager.GetInstance().ShowToast("分享视频失败！");
                    console.log(err);
                    if (on_failed) on_failed();
                },
            });
        } else {
            //ChannelManager.GetInstance().ShowToast("暂无可分享视频");
        }
    }
    public GetSystemInfo() :any{
        return tt.getSystemInfoSync();
    }
    public VibrateShort() {
        tt.vibrateShort({});
    }
    public VibrateLong() {
        tt.vibrateLong({});
    }
    public NavigateToMiniProgram(target: string, path: string, extraData: object, ON_SUCCESS: Function, ON_FAILED: Function, is_developer: boolean) {
        throw new Error("Method not implemented.");
    }
    public OnShow(callback: Function) {
        throw new Error("Method not implemented.");
    }
    public OnHide(callback: Function) {
        tt.onHide(callback);
    }
    public OffShow(callback: Function) {
        throw new Error("Method not implemented.");
    }
    public OffHide(callback: Function) {
        throw new Error("Method not implemented.");
    }
    public GetLaunchOptionsSync() {
        throw new Error("Method not implemented.");
    }
    public GetEnterOptionsSync() {
        throw new Error("Method not implemented.");
    }
    public ExitMiniProgram(on_successed: Function, on_failed: Function, on_complete: Function) {
        throw new Error("Method not implemented.");
    }


    private recorder = null;
    private on_record_stop: Function = null;
    /**
     * 初始化录屏
     */
    private InitRecorder() {
        this.recorder = tt.getGameRecorderManager();

        this.recorder.onStop(res => {
            if (this.GetCurrentTimestamp(true) - ChannelData.record_start_time >= 4000) {
                ChannelData.record_path = res.videoPath;
            }
            ChannelData.is_recording = false;
            if (this.on_record_stop) this.on_record_stop();
        });

        this.recorder.onStart(res => {
            ChannelData.is_recording = true;
            ChannelData.record_start_time = this.GetCurrentTimestamp(true);
        });

        this.recorder.onError(res => {
            console.log("------>录屏时发生错误<------\n" + res);
            ChannelData.is_recording = false;
        });
    }

    public navigateToScene() {
        console.log('OnSideClick', 2);
        tt.navigateToScene({
            scene: "sidebar",
            success: (res) => {
                console.log("navigate to scene success");
                // 跳转成功回调逻辑
            },
            fail: (res) => {
                console.log("navigate to scene fail: ", res);
                // 跳转失败回调逻辑
            },
        });
    }


    public CheckScene(success: Function, fail: Function, caller: any) {
        tt.checkScene({
            scene: "sidebar",
            success: (res) => {
                success?.call(caller, res.isExist);
                console.log("check scene success: ", res.isExist);
            },
            fail: (res) => {
                fail?.call(caller);
                console.log("check scene fail:", res);
            }
        })
    }

    public SetClipboardData(data: string, success: Function, fail: Function, complete: Function, caller: any) {
        tt.setClipboardData({
            data: data,
            success(res) {
                success?.call(caller);
            },
            fail(res) {
                fail?.call(caller);
            },
            complete(res) {
                complete?.call(caller);
            }
        });
    }
}